/* 	

	Fred 

	Implementation

	Created:  		November 1991
	Last Edited:		April 1992
	
	Sean Luke
	
	Modified	Todd Anthony Nathan
			April 10, 1992
			Cleaned up code, and got slider to work
	
*/


#import "MyView.h"
#define	DISTANCE	2.0

@implementation MyView

// Prototypes
DPSTimedEntryProc movewindow(DPSTimedEntry teNum,double now, void* TheWindow);

- appDidInit:sender
// Init some things for later use.
{
	interval	= 0.5;
	priority	= NX_RUNMODALTHRESHOLD;
	thisWindow	= [NXApp appIcon];
	// put in the first timed entry...
	teNum		= DPSAddTimedEntry((double) interval, (DPSTimedEntryProc) movewindow,
				(void*) thisWindow, (int) priority);
			
	return self;
	
}


- appWillTerminate:sender
// Clean up and go home when user quits
{
	if (teNum) DPSRemoveTimedEntry(teNum);
	
	return self;
	
}


- changeSpeed:sender
{
	interval = [theSlider floatValue];
	printf("The slider value is = %f\n", interval);
	if (teNum) DPSRemoveTimedEntry(teNum);
	teNum = DPSAddTimedEntry((double) interval, (DPSTimedEntryProc) movewindow,
			(void*) thisWindow, (int) priority);
			
	return self;
	
}
	
	
DPSTimedEntryProc movewindow(DPSTimedEntry teNum,double now, void* TheWindow)
{
	id	theWindow;
	int	z;
	float	x,
		y,
		a,
		b,
		width,
		height,
		angle;
	NXPoint	ThePoint;
	NXRect	TheRect;
	
	for (z = 0 ; z <= 20 ; z++) {
		theWindow = (id)TheWindow;
		[theWindow getMouseLocation:&ThePoint];
		x = ThePoint.x;
		y = ThePoint.y;
		[theWindow getFrame: &TheRect];
		a = TheRect.origin.x;
		b = TheRect.origin.y;
		width = TheRect.size.width;
		height = TheRect.size.height;
		angle = atan2( y - height / 2.0, x - width / 2.0);
		if ((x + y - height / 2.0 - width / 2.0 > 1.5) || (x + y - height / 2.0 - width / 2.0 < - 1.5))
			 [theWindow moveTo:DISTANCE * cos(angle) + a :DISTANCE * sin(angle) + b];
	}

	return (void*) NULL;

}

@end
